package net.fiodor.log {
	import net.fiodor.log.channel.IChannel;		

	public class Logger {

		private static var __loggerInstance : Logger;
		private var __logLevel : LogLevel = LogLevel.ALL;

		private var _loggerId : String;
		private var _publishers : Array;
		private var _filters : Array;

		public function setLogLevel( level : LogLevel ) : void { 
			__logLevel = level; 
		}

		public static function getLogger() : Logger {
			
			if(__loggerInstance == null) 
			__loggerInstance = new Logger("My logger");
			return __loggerInstance;
		}

		public function Logger( logId : String = "" ) {
			
			if( logId.length > 0 ) this._loggerId = logId.split(":")[logId.split(":").length - 1];
			
			__logLevel = LogLevel.LOG;
			_publishers = new Array();
			_filters = new Array();
		}

		public function addChannel( publisher : IChannel ) : void {
			if( !_publishers[publisher.toString()] ) _publishers[publisher.toString()] = publisher;
		}

		public function removeChannel( publisher : IChannel ) : void {
			delete _publishers[publisher.toString()];
		}

		public function getPublishers() : Array { 
			return _publishers; 
		}

		private function publish( argument : Object, level : LogLevel ) : void {
			if( level.getValue() >= Log.getLogLevel().getValue() ) {
				var e : LogEvent = new LogEvent(this._loggerId, argument, level);
				for( var publisher:String in _publishers ) {
					IChannel(_publishers[publisher]).publish(e);
				}
			}
		}

		/* log functions */

		public function log( argument : Object ) : void { 
			publish(argument, LogLevel.LOG); 
		}

		public function debug( argument : Object ) : void { 
			publish(argument, LogLevel.DEBUG); 
		}

		public function info( argument : Object ) : void { 
			publish(argument, LogLevel.INFO); 
		}

		public function warn( argument : Object ) : void { 
			publish(argument, LogLevel.WARN); 
		}

		public function error( argument : Object ) : void { 
			publish(argument, LogLevel.ERROR); 
		}

		public function fatal( argument : Object ) : void { 
			publish(argument, LogLevel.FATAL); 
		}
	}
}